package com.study.component1;

import javax.annotation.PostConstruct;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @Author tanglingyu
 * @Date: 2021/09/08/ 19:45
 * @Description
 */
@Component
//@Order(100) 比较SpringUtils
public class TestComponent2 implements ApplicationContextAware {

  private ApplicationContext applicationContext;

  /**
   * AbstractAutowireCapableBeanFactory
   * @Order在没有implements Aware接口的时候越小越先加载
   * 如果implements Aware接口那么@Order会失效并且默认是implements Aware的类晚于没有implements Aware的类加载
   * 推荐本类implements ApplicationContextAware注入applicationContext对象
   */
  @PostConstruct
  public void test() {
//    final PrimaryAnnotation bean1 = applicationContext.getBean(PrimaryAnnotation.class);
//    System.out.println(bean1);
//    final PrimaryAnnotation bean = SpringUtils.getBean(PrimaryAnnotation.class);
//    System.out.println(bean);
//    final PrimaryAnnotation get1 = SpringUtils.getBean("get1", PrimaryAnnotation.class);
//    System.out.println(get1);
//    final PrimaryAnnotation get2 = SpringUtils.getBean("get2", PrimaryAnnotation.class);
//    System.out.println(get2);
    System.out.println("2");
  }

  @Override
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    this.applicationContext = applicationContext;
  }
}
